using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Media3D;
using System.Threading;


namespace C4F.VistaP2P.WPF
{
    /// <summary>
    /// Interaction logic for MediaElementViewPort.xaml
    /// </summary>

    public partial class MediaElementViewPort : System.Windows.Controls.UserControl
    {
        ModelVisual3D mMediaElementModel;        
        

        public MediaElement MediaElement
        {
            get
            {
                return mVideoPlayer;
            }
        }

        public MediaElementViewPort()
        {
            InitializeComponent();
        
            //////////////////////////////////////////////////////////////////////
            //This is where we create the Plane and brushes to make the 3d looking Video Player.
            //////////////////////////////////////////////////////////////////////
            Plane p = new Plane();
            mMediaElementModel = new ModelVisual3D();
            p.UpperLeft = new Point3D(-6.4, -4.8, 0);
            p.LowerRight = new Point3D(6.4, 4.8, 0);
            VisualBrush vb = new VisualBrush(mVideoPlayer);
            MaterialGroup mg = new MaterialGroup();
            mg.Children.Add(new DiffuseMaterial(vb));

            GeometryModel3D gm3d = new GeometryModel3D(p.Mesh, new DiffuseMaterial(new SolidColorBrush(Colors.Black)));
            gm3d.Material = mg;
            mMediaElementModel.Content = gm3d;
            this.mainViewport.Children.Add(mMediaElementModel);
            mVertAngle = -10;
            mHorzAngle = 10;

        }



        protected void OnFailedVideo(object sender, System.Windows.RoutedEventArgs e)
        {           
            MediaElement.Close();
        }

        protected void OnEndedVideo(object sender, System.Windows.RoutedEventArgs e)
        {
         
        }

        protected void OnBufferStartVideo(object sender, System.Windows.RoutedEventArgs e)
        {
           
        }

        protected void OnBufferEndVideo(object sender, System.Windows.RoutedEventArgs e)
        {
        }



        /// <summary>
        /// The slider is mapped to this function and changes the angle of the video camera by rotating the panel 
        /// in the mainviewport that the media element is textured on.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        double mVertAngle;
        protected void OnSliderChangedVert(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            mVertAngle = -e.NewValue;
            if (null != mMediaElementModel)
            {
                Rotation3D horz = new AxisAngleRotation3D(new Vector3D(0, 1, 0), (mHorzAngle - 10) * 5);
                Rotation3D vert = new AxisAngleRotation3D(new Vector3D(-1, 0, 0), (mVertAngle + 10) * 5);
                Transform3D horzTransform3d = new RotateTransform3D(horz, new Point3D(5, 0, 0));
                Transform3D vertTransform3d = new RotateTransform3D(vert, new Point3D(0, -5, 0));

                Transform3DGroup mt3dGroup = new Transform3DGroup();
                mt3dGroup.Children.Add(horzTransform3d);
                mt3dGroup.Children.Add(vertTransform3d);

                //Matrix3D result = horzTransform3d.Value * vertTransform3d.Value;

                Transform3D t3d = new RotateTransform3D();                
                mMediaElementModel.Transform = mt3dGroup;
                
            }
        }

        /// <summary>
        /// The slider is mapped to this function and changes the angle of the video camera by rotating the panel 
        /// in the mainviewport that the media element is textured on.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// 
        double mHorzAngle;
        protected void OnSliderChangedHorz(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            mHorzAngle = e.NewValue;
            if (null != mMediaElementModel)
            {
                Rotation3D horz = new AxisAngleRotation3D(new Vector3D(0, 1, 0), (mHorzAngle - 10) * 5);
                Rotation3D vert = new AxisAngleRotation3D(new Vector3D(-1, 0, 0), (mVertAngle + 10) * 5);                        
                Transform3D horzTransform3d = new RotateTransform3D(horz, new Point3D(5, 0, 0));
                Transform3D vertTransform3d = new RotateTransform3D(vert, new Point3D(0, -5, 0));

                Transform3DGroup mt3dGroup = new Transform3DGroup();
                mt3dGroup.Children.Add(horzTransform3d);
                mt3dGroup.Children.Add(vertTransform3d);

                //Matrix3D result = horzTransform3d.Value * vertTransform3d.Value;
               
                Transform3D t3d = new RotateTransform3D();                
                mMediaElementModel.Transform = mt3dGroup;
                
            }
        }

    }
}